<template>
	<view class="u-wrap">
		<u-navbar :is-back="false" :background="background">
			<view class="slot-wrap">
				<view class="search-wrap" @click="goSearch()">
					<view class="u-search-inner">
						<u-icon name="search" color="#909399" :size="28"></u-icon>
						<text class="u-search-text">搜索商品</text>
					</view>
				</view>
				<view class="navbar-right">
					<!-- view class="message-box right-item" @click="tell">
						<u-icon name="phone" size="38" color="#333333"></u-icon>
					</view> -->
				</view>
			</view>
		</u-navbar>
		<view class="u-menu-tab"><u-tabs :list="oneList" :current="oneCurrent" @change="oneChange"></u-tabs></view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view
					v-for="(item, index) in twoList"
					:key="index"
					class="u-tab-item"
					:class="[twoCurrent == index ? 'u-tab-item-active' : '']"
					:data-current="index"
					@tap.stop="swichMenu(index)"
				>
					<text class="u-line-1">{{ item.name }}</text>
				</view>
			</scroll-view>

			<scroll-view scroll-y class="right-box" @scrolltolower="reachBottom">
				<view class="page-view" v-if="goodsList.length > 0">
					<view class="item-container class-item">
						<block v-for="(item, index) in goodsList" :key="index" >
							<view class="thumb-box" @tap="goGoods(item.id)">
								<view class="thumb-box-left">
									<image class="item-menu-image" v-if="item.img" :src="item.img" mode=""></image>
									<image class="item-menu-image" v-else src="https://uviewui.com/common/logo.png" mode=""></image>
								</view>
								<view class="thumb-box-right">
									<view class="item-menu-name u-line-2">{{ item.name }}</view>
									<view class="item-menu-unit u-line-1">{{ item.toreInfo }}</view>
									<view class="item-menu-price u-line-1">
										<view>
											￥{{ item.originalPrice != null ? item.originalPrice : '暂无' }}
											<!-- <text class="unit">原价:{{ item.originalPrice != null ? item.originalPrice : '未认证' }}</text> -->
										</view>
							
										<view><u-icon name="plus-circle-fill" color="#17933f" size="46"></u-icon></view>
									</view>
								</view>
							</view>
						</block>
						
					</view>
					<!-- <u-loadmore class="loadmore" status="nomore" icon-type="flower" /> -->
					<u-loadmore class="loadmore" :status="loadStatus" icon-type="flower"></u-loadmore>
				</view>
				<u-empty v-else text="暂无商品信息" mode="list"></u-empty>
			</scroll-view>
		</view>
		<view><u-toast ref="uToast" /></view>
	</view>
</template>

<script>
import classifyData from '@/common/classify.data.js';
var page = 0,
	size = 20;
export default {
	data() {
		return {
			background: {
				//backgroundImage: 'linear-gradient(90deg, rgb(0,164,133), rgb(51,241,205))'
			},
			//
			number: 0,
			totalPages: 0,
			//
			tabbar: classifyData,
			scrollTop: 0, //tab标题的滚动条位置
			current: 0, // 预设当前项的值
			menuHeight: 0, // 左边菜单的高度
			menuItemHeight: 0, // 左边菜单item的高度
			//一级分类
			oneList: [], //一级分类
			oneCurrent: 0,
			oneIndexId: 0,
			//二级分类
			twoList: [], //二级分类
			twoCurrent: 0,
			//商品
			goodsList: [],
			//
			loadStatus: 'loadmore', //加载更多
			//
			userInfo: {}
		};
	},
	computed: {},
	onLoad(option) {
		/* 	uni.removeStorageSync('token');
		uni.removeStorageSync('userInfo');
		uni.clearStorageSync(); */
		try {
			const token = uni.getStorageSync('token');
			if (token) {
				this.userInfo = uni.getStorageSync('userInfo');
				if(!this.userInfo){
					//#ifdef APP-PLUS
					uni.reLaunch({
						url: '/pages/login/index'
					});
					//#endif
					//#ifdef MP-WEIXIN
					uni.reLaunch({
						url: '/pages/login/xcxLogin'
					});
					//#endif
				}
				this.getOneCategory();
			} else {
				//#ifdef APP-PLUS
				uni.reLaunch({
					url: '/pages/login/index'
				});
				//#endif
				//#ifdef MP-WEIXIN
				uni.reLaunch({
					url: '/pages/login/xcxLogin'
				});
				//#endif
			}
		} catch (e) {
			// error
		}
	},
	onShow() {
		try {
			const cateId = uni.getStorageSync('cateId');
			console.log(cateId);
			if (cateId) {
				this.oneList.forEach((item, index) => {
					if (item.id == cateId) {
						//this.oneCurrent = index;
						this.oneChange(index);
						console.log(index);
						return;
					}
				});
				uni.removeStorageSync('cateId');
			}
		} catch (e) {
			// error
		}
	},
	methods: {
		//一级分类
		getOneCategory() {
			this.$u.api.getOneCategory().then(res => {
				this.oneList = res.data;
				this.getTwoCategory(this.oneList[this.oneCurrent].id);
			});
		},
		//
		oneChange(index) {
			this.oneCurrent = index;
			this.twoCurrent = 0;
			this.initDate();
			this.getTwoCategory(this.oneList[this.oneCurrent].id);
		},
		initDate() {
			this.goodsList = [];
			this.twoCurrent = 0;
		},
		//二级分类
		getTwoCategory(upId) {
			this.$u.api.getTwoCategory({ upId: upId }).then(res => {
				this.twoList = res.data.content;
				/* console.log('twoList');
				console.log(this.twoList); */
				if (this.twoList.length == 0) {
					this.twoList = [];
					this.goodsList = [];
				} else {
					console.log(this.twoCurrent);
					this.getNewGoods(this.twoList[this.twoCurrent].id);
				}
			});
		},
		//商品列表
		getNewGoods(categoryId) {
			page = 0;
			this.loadStatus = 'loading';
			this.$u.api.getGoodsList({ categoryId: categoryId, page: page, size: size, sort: 'id' }).then(res => {
				this.goodsList = res.data.content;
				let totalPages = res.data.totalPages;
				let number = res.data.number;
				page++;
				if (page < totalPages) {
					this.loadStatus = 'loadmore';
				} else {
					this.loadStatus = 'nomore';
				}
				console.log('getGoodsList');
				console.log(res);
			});
		},
		getGoods(categoryId) {
			this.loadStatus = 'loading';
			this.$u.api.getGoodsList({ categoryId: categoryId, page: page, size: size, sort: 'id' }).then(res => {
				this.goodsList = this.goodsList.concat(res.data.content);
				//this.goodsList = res.data.content;
				let totalPages = res.data.totalPages;
				let number = res.data.number;
				page++;
				if (page < totalPages) {
					this.loadStatus = 'loadmore';
				} else {
					this.loadStatus = 'nomore';
				}
				console.log('getGoodsList');
				console.log(res);
			});
		},
		//跳转详情
		goGoods(goodsId) {
			if (this.userInfo.realName == false) {
				this.$refs.uToast.show({
					title: '当前用户未认证，无法查看更多内容',
					type: 'error'
				});
			} else {
				this.$u.route({
					type: 'to',
					params: {
						goodsId: goodsId
					},
					url: '/pages/goods/index',
				});
			}
		},
		// 点击左边的栏目切换
		async swichMenu(index) {
			//
			console.log('swichMenu');
			console.log(index);
			console.log(this.twoList);
			this.getNewGoods(this.twoList[index].id);
			if (index == this.twoCurrent) return;
			this.twoCurrent = index;
			// 如果为0，意味着尚未初始化
			if (this.menuHeight == 0 || this.menuItemHeight == 0) {
				await this.getElRect('menu-scroll-view', 'menuHeight');
				await this.getElRect('u-tab-item', 'menuItemHeight');
			}
			// 将菜单菜单活动item垂直居中
			this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
		},
		// 获取一个目标元素的高度
		getElRect(elClass, dataVal) {
			new Promise((resolve, reject) => {
				const query = uni.createSelectorQuery().in(this);
				query
					.select('.' + elClass)
					.fields({ size: true }, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					})
					.exec();
			});
		},
		//上拉
		reachBottom() {
			// 此tab为空数据
			console.log('上拉============');
			this.loadStatus = 'loading';
			setTimeout(() => {
				this.getGoods(this.twoList[this.twoCurrent].id);
			}, 1200);
		},
		goSearch() {
			console.log('goSearch');
			uni.navigateTo({
				url: '/pages/search/index'
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.u-wrap {
	height: calc(100vh - 8px);
	/* #ifdef H5 */
	height: calc(100vh - 40px);
	/* #endif */
	display: flex;
	flex-direction: column;
}

.slot-wrap {
	display: flex;
	align-items: center;
	flex: 1;
	.search-wrap {
		padding: 18rpx 20rpx;
		flex: 1;
		.u-search-inner {
			background-color: rgb(234, 234, 234);
			border-radius: 100rpx;
			display: flex;
			align-items: center;
			padding: 10rpx 16rpx;
		}
	
		.u-search-text {
			font-size: 26rpx;
			color: $u-tips-color;
			margin-left: 10rpx;
		}
	}
}
.u-search-box {
	padding: 18rpx 30rpx;
}

.u-menu-wrap {
	flex: 1;
	display: flex;
	overflow: hidden;
}

.u-search-inner {
	background-color: rgb(234, 234, 234);
	border-radius: 100rpx;
	display: flex;
	align-items: center;
	padding: 10rpx 16rpx;
}

.u-search-text {
	font-size: 26rpx;
	color: $u-tips-color;
	margin-left: 10rpx;
}

.u-tab-view {
	width: 200rpx;
	height: 100%;
}

.u-tab-item {
	height: 110rpx;
	background: #f6f6f6;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 24rpx;
	color: #444;
	font-weight: 400;
	line-height: 1;
}

.u-tab-item-active {
	position: relative;
	color: #000;
	font-size: 30rpx;
	font-weight: 600;
	background: #fff;
}

.u-tab-item-active::before {
	content: '';
	position: absolute;
	border-left: 4px solid $u-type-primary;
	height: 32rpx;
	left: 0;
	top: 39rpx;
}

.u-tab-view {
	height: 100%;
}

.right-box {
	background-color: rgb(234, 234, 234);
}

.page-view {
	padding: 0;
}
.loadmore {
	padding: 16rpx 0;
}
.class-item {
	background-color: #fff;
	padding: 20rpx;
	border-radius: 8rpx;
}

.item-title {
	font-size: 26rpx;
	color: $u-main-color;
	font-weight: bold;
}

.item-container {
	display: flex;
	flex-wrap: wrap;
}

.thumb-box {
	width: 100%;
	display: flex;
	align-items: l;
	justify-content: flex-start;
	margin: 10rpx 6rpx;
}
.thumb-box-left {
	width: 30%;
}
.item-menu-image {
	width: 120rpx;
	height: 120rpx;
}
.thumb-box-right {
	width: 70%;
}
.item-menu-name {
	font-weight: bold;
	font-size: 26rpx;
	color: $u-main-color;
}
.item-menu-unit {
	padding-top: 6rpx;
	font-size: 22rpx;
	color: $u-content-color;
}
.item-menu-price {
	padding-top: 10rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	color: $u-type-error;
}
.unit {
	padding-left: 6rpx;
	font-size: 24rpx;
	color: #c8c9cc;
	font-weight: normal;
}
</style>
